﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace System
{
    public class OracleDal<T> : BaseDAL<T> where T : BaseModel, new()
    {
        public override DBType DBType
        {
            get
            {
                return DBType.Oracle;
            }

        }

        public override List<Tm> _GetList<Tm>(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, List<DBParameter> parameters)
        {
            if (parameters == null) parameters = new List<DBParameter>();

            recordCount = GetCount(filter, parameters);

            Tm model = new Tm();

            string primaryKey = model.GetPrimaryKey();

            parameters = InitDataParameter(parameters, pageIndex, pageSize);

            string sql = SqlHelper.GetPageViewSql(pageIndex, pageSize, fields, filter, sorts, tableName, primaryKey, this.DBType);

            List<Tm> list = null;

            using (IDataReader dr = ExecuteReader(parameters, sql))
            {
                list = _GetList<Tm>(dr);
            }

            return list;

        }
        protected override DataSet GetDataSet(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, string primaryKey, List<DBParameter> parameters)
        {
            if (parameters == null) parameters = new List<DBParameter>();

            recordCount = GetCount(filter, parameters);

            parameters = InitDataParameter(parameters, pageIndex, pageSize);

            DataSet ds = null;

            string sql = SqlHelper.GetPageViewSql(pageIndex, pageSize, fields, filter, sorts, tableName, primaryKey, this.DBType);
            ds = ExecuteDataSet(sql, parameters, CommandType.Text);
            
            return ds;
        }

        protected override List<DBParameter> InitDataParameter(List<DBParameter> parameters, int? pageIndex, int? pageSize)
        {
            if (parameters == null)
                parameters = new List<DBParameter>();

            DBParameter top = new DBParameter();
            top.DbType = DbType.Int32;
            top.ParameterName = "pageIndex";
            top.Value = pageIndex;
            parameters.Add(top);

            DBParameter size = new DBParameter();
            size.DbType = DbType.Int32;
            size.ParameterName = "pageSize";
            size.Value = pageSize;
            parameters.Add(size);

            return parameters;
        }
    }
}
